package ui.activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mynewsapp.R;
import com.github.appintro.AppIntro;
import com.github.appintro.AppIntroFragment;
import com.github.appintro.AppIntroPageTransformerType;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import ui.activity.AccountActivity;
import ui.activity.MainActivity;


// ref link:
// https://github.com/AppIntro/AppIntro/blob/main/example/src/main/java/com/github/appintro/example/ui/java/JavaIntro.java#L12
public class SplashActivity extends AppIntro {

    private static final String PREFS_NAME = "AppIntroPrefs";
    private static final String PREFS_KEY = "isAppIntroShown";
    private static final String PREFS_USER_LOGIN = "isUserLoggedIn";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 检查是否已经显示过引导页
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean isAppIntroShown = prefs.getBoolean(PREFS_KEY, false);


        if (isAppIntroShown) {
            // 如果已经显示过，直接跳转到登录或主页面
            navigateToNextScreen();
            finish();
            return;
        }


        addSlide(AppIntroFragment.createInstance("欢迎使用!",
                "本应用承诺免费!",
                R.drawable.splash_page_notice,
                R.color.splash_page_color
        ));

        addSlide(AppIntroFragment.createInstance(
                "但是",
                "你需要遵守约定",
                R.drawable.splash_page_notice,
                R.color.splash_page_color
        ));

        addSlide(AppIntroFragment.createInstance(
                "第一条",
                "你必须同意隐私政策",
                R.drawable.splash_page_notice,
                R.color.splash_page_color
        ));

        addSlide(AppIntroFragment.createInstance(
                "第二条",
                "本应用收集必要数据除隐私数据以维护公司利益，你必须同意才能运行app",
                R.drawable.splash_page_notice,
                R.color.splash_page_color
        ));

        // Fade Transition
        setTransformer(AppIntroPageTransformerType.Fade.INSTANCE);

        // Show/hide status bar
        showStatusBar(true);
        //Enable the color "fade" animation between two slides (make sure the slide implements SlideBackgroundColorHolder)
        setColorTransitionsEnabled(true);

        //Prevent the back button from exiting the slides
        setSystemBackButtonLocked(true);

        //Activate wizard mode (Some aesthetic changes)
        setWizardMode(true);

        //Enable immersive mode (no status and nav bar)
        setImmersiveMode();

        //Enable/disable page indicators
        setIndicatorEnabled(true);

        //Dhow/hide ALL buttons
        setButtonsEnabled(true);

        // Enable Vibration
        setVibrate(true);


        // 授权
        grantPermission();


    }

    @Override
    protected void onSkipPressed(Fragment currentFragment) {
        super.onSkipPressed(currentFragment);
        markAppIntroShown();
        navigateToNextScreen();
        finish();
    }

    @Override
    protected void onDonePressed(Fragment currentFragment) {
        super.onDonePressed(currentFragment);
        markAppIntroShown();
        navigateToNextScreen();
    }

    private void markAppIntroShown() {
        // 标记引导页已显示
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(PREFS_KEY, true);
        editor.apply();
    }

    private void navigateToNextScreen() {
        // 检查用户是否已经登录
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean isUserLoggedIn = prefs.getBoolean(PREFS_USER_LOGIN, false);

        Intent intent;
        if (isUserLoggedIn) {
            // 如果用户已登录，跳转到主页面
            intent = new Intent(this, MainActivity.class);
        } else {
            // 如果用户未登录，跳转到登录页面
            // todo: 应该加载AccountActivity的
            intent = new Intent(this, AccountActivity.class);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    // 处理授权
    private void grantPermission() {
        String[][] toGrant = new String[][]{
                Permission.Group.LOCATION,
                Permission.Group.STORAGE,
                Permission.Group.CALENDAR,
                Permission.Group.CAMERA,
                Permission.Group.PHONE,
                Permission.Group.MICROPHONE,
                Permission.Group.CONTACTS,
                Permission.Group.SENSORS,
                Permission.Group.SMS,

        };
        AndPermission.with(this)
                .runtime()
                .permission(toGrant)
                .onGranted(permissions -> {
                    Toast.makeText(this, "权限ok", Toast.LENGTH_SHORT).show();
                }).onDenied(permissions -> {
                    Toast.makeText(this, "权限error", Toast.LENGTH_SHORT).show();
                })
                .start();
    }
}